import scrapy
from json import loads
from scrapy.http import Request


class TttSpider(scrapy.Spider):
    name = 'ttt'
    allowed_domains = ['dbbqb.com']
    # start_urls = ['http://www.dbbqb.com/api/group/241/detail']
    # start_urls = ['http://www.dbbqb.com/api/search/json?start=0&w=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80']

    def start_requests(self):
        # start_urls = 'http://www.dbbqb.com/api/search/json?start=0&w=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80'
        start_urls = 'http://www.dbbqb.com/api/group/241/detail'
        headers ={
            'Web-Agent': 'web'
        }
        yield Request(start_urls, headers=headers)

    def parse(self, response):
        data = loads(response.text)
        pre_urls = "http://image.dbbqb.com/"
        i = 0
        for d in data.get("details"):

            img_urls = pre_urls+d.get("path")
            images = d.get("desc")
            if images==None:
                images = f"图片{i}.jpg"
                i += 1
            yield {
                'image_urls': img_urls,
                'images': images
            }
